package com.whz.bus.debezium;

import io.debezium.connector.mysql.MySqlConnector;
import io.debezium.connector.postgresql.PostgresConnector;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.util.Assert;

@Data
@AllArgsConstructor
public class DataSourceWrapper {

    private DataSourceProperties dataSourceProperties;

    private DatabaseDriver getDatabaseDriver() {
        Assert.notNull(dataSourceProperties, "dataSourceProperties not null.");
        return DatabaseDriver.fromJdbcUrl(dataSourceProperties.getUrl());
    }

    public boolean isPG() {
        return getDatabaseDriver() == DatabaseDriver.POSTGRESQL;
    }

    public boolean isMySql() {
        return getDatabaseDriver() == DatabaseDriver.MYSQL;
    }


    public String getConnector() {
        if (isMySql()) {
            return MySqlConnector.class.getName();
        }
        if (isPG()) {
            return PostgresConnector.class.getName();
        }
        throw new RuntimeException("Not Supported Connector type");
    }
}